﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpportunityCustomerIdFix.Controller;
using System.IO;
using System.Diagnostics;

namespace OpportunityCustomerIdFix
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
        }

        // Opens a File Dialog box for the user to select the desired zip file to process
        // Processes the selected file
        private void Browse_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK) // Test result.
            {
                string filePath = openFileDialog1.FileName;
                ProcessFile(filePath);
            }
        }

        // Execute the same logic as Browse_Click
        private void FilePath_Click(object sender, EventArgs e)
        {
            Browse_Click(sender, e);
        }

        // Closes the application
        private void Close_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        // Allows the user to process a zip file by dragging it onto the application
        private void MainForm_DragDrop(object sender, DragEventArgs e)
        {
            Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
            if (a != null)
            {
                string filePath = a.GetValue(0).ToString();
                ProcessFile(filePath);
            }
        }

        // Allows for the use of Drag and Drop on the form
        private void MainForm_DragEnter(object sender, DragEventArgs e)
        {
            Array a = (Array)e.Data.GetData(DataFormats.FileDrop);
            if (e.Data.GetDataPresent(DataFormats.FileDrop) && a.Length == 1)
            {
                e.Effect = DragDropEffects.Copy;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        // Initiates the processing of the zip file and displays an appropriate message when completed
        public void ProcessFile(string filePath)
        {
            this.FilePath.Text = Path.GetFileName(filePath);

            string messageText = null;
            Color messageColor = Color.Red;
            try
            {
                //Initialize controller
                CustomerIdFixController CustomerIdFix = new CustomerIdFixController(filePath);

                //Execute the steps to unzip, process, and rezip the file
                if (CustomerIdFix.UnzipFile())
                {
                    messageText = "File processed successfully.";
                    messageColor = Color.Green;
                }
                else
                {
                    messageText = "No changes necessary.";
                    messageColor = Color.Green;
                }
            }
            catch (Exception ex)
            {
                messageText = "Error: Please ensure correct file is selected.";
                messageColor = Color.Red;
            }
            finally
            {
                this.MessageResponse.Text = messageText;
                this.MessageResponse.ForeColor = messageColor;
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {
            Process.Start("mailto:nathan@magnetismsolutions.com");
        }
    }
}
